package cn.changein.framework.modular.gen.generator.custom;

import cn.changein.framework.modular.gen.generator.base.model.ContextParam;
import cn.changein.framework.modular.gen.generator.custom.controller.ControllerGenerator;
import cn.changein.framework.modular.gen.generator.custom.html.PageAddGenerator;
import cn.changein.framework.modular.gen.generator.custom.html.PageEditGenerator;
import cn.changein.framework.modular.gen.generator.custom.html.PageIndexGenerator;
import cn.changein.framework.modular.gen.generator.custom.js.PageAddJsGenerator;
import cn.changein.framework.modular.gen.generator.custom.js.PageEditJsGenerator;
import cn.changein.framework.modular.gen.generator.custom.js.PageIndexJsGenerator;
import cn.changein.framework.modular.gen.generator.custom.mybatisplus.CustomMpGenerator;
import cn.changein.framework.modular.gen.generator.custom.sql.MenuSqlGenerator;
import cn.changein.framework.modular.gen.generator.restful.mybatisplus.param.MpParam;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;

import java.util.List;
import java.util.Map;

/**
 * 测试的执行器
 *

 * @date: 2020-02-17
 * @copyright: 广州澄境科技有限公司
 */
public class CustomExecutor {
    /**
     * 默认的生成器
     * @param contextParam
     * @param mpContext
     */
    public static void executor(ContextParam contextParam, MpParam mpContext) {

        //执行mp的代码生成，生成entity,dao,service,model，生成后保留数据库元数据
        CustomMpGenerator MpGenerator = new CustomMpGenerator(mpContext);
        MpGenerator.initContext(contextParam);
        MpGenerator.doGeneration();

        //获取元数据
        List<TableInfo> tableInfos = MpGenerator.getTableInfos();
        Map<String, Map<String, Object>> everyTableContexts = MpGenerator.getEveryTableContexts();

        //遍历所有表
        for (TableInfo tableInfo : tableInfos) {
            Map<String, Object> map = everyTableContexts.get(tableInfo.getName());

            //生成控制器
            ControllerGenerator ControllerGenerator = new ControllerGenerator(map);
            ControllerGenerator.initContext(contextParam);
            ControllerGenerator.doGeneration();

            //生成主页面html
            PageIndexGenerator PageIndexGenerator = new PageIndexGenerator(map);
            PageIndexGenerator.initContext(contextParam);
            PageIndexGenerator.doGeneration();

            //生成主页面js
            PageIndexJsGenerator PageIndexJsGenerator = new PageIndexJsGenerator(map);
            PageIndexJsGenerator.initContext(contextParam);
            PageIndexJsGenerator.doGeneration();

            //生成添加页面html
            PageAddGenerator PageAddGenerator = new PageAddGenerator(map);
            PageAddGenerator.initContext(contextParam);
            PageAddGenerator.doGeneration();

            //生成添加页面的js
            PageAddJsGenerator PageAddJsGenerator = new PageAddJsGenerator(map);
            PageAddJsGenerator.initContext(contextParam);
            PageAddJsGenerator.doGeneration();

            //生成编辑页面html
            PageEditGenerator PageEditGenerator = new PageEditGenerator(map);
            PageEditGenerator.initContext(contextParam);
            PageEditGenerator.doGeneration();

            //生成编辑页面的js
            PageEditJsGenerator PageEditJsGenerator = new PageEditJsGenerator(map);
            PageEditJsGenerator.initContext(contextParam);
            PageEditJsGenerator.doGeneration();

            //生成菜单的sql
            MenuSqlGenerator MenuSqlGenerator = new MenuSqlGenerator(map);
            MenuSqlGenerator.initContext(contextParam);
            MenuSqlGenerator.doGeneration();
        }
    }

    public static void main(String[] args) {

        ContextParam contextParam = new ContextParam();

        contextParam.setJdbcDriver("com.mysql.jdbc.Driver");
        contextParam.setJdbcUserName("root");
        contextParam.setJdbcPassword("root");
        contextParam.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/generator_platform?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=CTT");
        contextParam.setOutputPath("temp");
        contextParam.setAuthor("changein");
        contextParam.setProPackage("cn.changein..modular.test");

        MpParam mpContextParam = new MpParam();
        mpContextParam.setGeneratorInterface(true);
        mpContextParam.setIncludeTables(new String[]{"test"});
        mpContextParam.setRemoveTablePrefix(new String[]{"sys_"});

        CustomExecutor.executor(contextParam, mpContextParam);
    }
}
